<?php
// @formatter:off
/**
 * @file c.collections.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-06-16 23:56:49 +0000 (Sun, 16 Jun 2013) $
 *
 * $Id: c.collections.php 73 2013-06-16 23:56:49Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0.0.1/themes/mdefault/controlers/contents/c.collections.php $
 */
// @formatter:on

if($wcProfile->allowedTo(WC_PERM_COLLECTION_LIST)) {
	require_once "{$this->_themeRoot}/controlers/generics/pager.php";

	global $wcCollectionsHolder;
	global $wctDefaults;
	global $wcPageCache;

	$wcPageCache->anyCollection();

	$page = isset($_REQUEST["page"]) ? $_REQUEST["page"] : 1;
	$minCollectionInPage = $wctDefaults["max-per-page"]["collections"] * ($page - 1);
	$maxCollectionInPage = $minCollectionInPage + $wctDefaults["max-per-page"]["collections"];

	$wcThemeAssigns["PATH"] = "Top Collections (page {$page})";
	$wcThemeAssigns["ACTIONCONTENT"] = "/generics/list-view.html";

	//
	// Selecting only top collections in range.
	$collections = array();
	$items = $wcCollectionsHolder->topItems($wcGroup);
	$collectionCount = 0;
	uasort($items, "WCCollection::NameSort");
	foreach($items as $collection) {
		if($collection->ok()) {
			if($collectionCount >= $minCollectionInPage && $collectionCount < $maxCollectionInPage) {
				$collections[] = $collection;
			}
			$collectionCount++;
		}
	}

	$pages = ceil($collectionCount / $wctDefaults["max-per-page"]["collections"]);
	wcSetPager(requestParams(true, $wcAction, array('page')), $pages, $page);
	if($pages > 1) {
		$wcThemeAssigns["PAGER-SHOW"] = true;
		$wcThemeAssigns["PAGER-CURRENT"] = $page;
		$wcThemeAssigns["PAGER-COUNT"] = $pages;
	}

	//
	// Creating multi-array by prefix
	$lists = array();
	$ids = 1;
	$gid = $wcGroup->id();
	$iconId = 1;
	$zoom = array();
	foreach($collections as $collection) {
		$name = $collection->name;
		$prefix = strtoupper(isset($name[0]) ? $name[0] : "#");
		if($prefix < 'A' || $prefix > 'Z') {
			$prefix = '#';
		}
		$prefix = "{$prefix}";
		if(!isset($lists[$prefix])) {
			$lists[$prefix] = array(
				"prefix" => $prefix,
				"list" => array()
			);
		}

		$lists[$prefix]["list"][] = array(
			"name" => $collection->name,
			"title" => $collection->name,
			"iconid" => $iconId,
			"icon" => $collection->preview(),
			"url" => "?action=".WC_ACTION_COLLECTION_SHOW."&group={$gid}&collection={$collection->id}"
		);

		$zoom[] = array(
			"id" => $iconId,
			"path" => $collection->preview()
		);

		$iconId++;
	}
	ksort($lists);

	if(count($collections) == 0) {
		$lists[] = array(
			"prefix" => "No available collections",
			"list" => array()
		);
	}

	$wcThemeAssigns["MenuLists"] = $lists;
	$wcThemeAssigns["MenuListsZoom"] = $zoom;
} else {
	$wcThemeAssigns["error-string"] = "You're not allowed to list collections";
	require "{$this->_themeRoot}/controlers/contents/c.403.php";
}
?>